package com.ybkj.daijia.api.apirest.v4;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.driver.transport.ResultTransport;
import com.ybkj.daijia.api.driver.v4.DriverAccountController;
import com.ybkj.daijia.api.util.ErrorConstantV4;
import com.ybkj.daijia.server.event.model.ActivityCheckEvent;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.reactor.ReactorVo;
import com.ybkj.daijia.server.sales.Activity.ActivityEvent;
import com.ybkj.daijia.server.service.ActivityService;
import com.ybkj.daijia.server.service.FirstOrderPassengerService;
import com.ybkj.daijia.server.service.MemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller("ApiActivityProcessControllerV4")
@RequestMapping(value = "api/activity/v4")
public class ActivityProcessController {

    private static final Logger logger = LoggerFactory.getLogger(DriverAccountController.class);

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @Autowired
    private FirstOrderPassengerService firstOrderPassengerService;

    @Autowired
    private ActivityService activityService;

    /**
     * 新客户活动
     *
     * @return
     */
    @RequestMapping(value = "newPassengerActivity", method = RequestMethod.POST)
    public @ResponseBody
    ResultTransport newPassengerActivity(Long passengerId) {

        if (null == passengerId) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
        }

        Passenger passenger = memberService.findOnePassenger(passengerId);
        if (null == passenger) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PASSENGER_NOT_EXISTS);
        }

        try {

            ReactorVo rVo = new ReactorVo();
            rVo.setPassenger(passenger);
            rVo.setActivityEvent(ActivityEvent.newRegister);
            ActivityCheckEvent activityCheckEvent = new ActivityCheckEvent(rVo);
            applicationEventMulticaster.multicastEvent(activityCheckEvent);

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            return instance;

        } catch (Exception e) {
            e.printStackTrace();
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }

    }


}
